package com.kasax.hilog;

import android.app.Activity;
import android.content.Context;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.kasax.utils.AppActivityManager;

public class ViewHelper {
    private static ScrollView scrollView;
    private static LinearLayout lineContainer;

    public static void showDeugLogInScrollView(Context appContext, String msg) {
        Activity topActivity = AppActivityManager.getInstance().getTopActivity();
        if (topActivity == null) return;

        ViewGroup androidContentContainer = topActivity.findViewById(android.R.id.content);
        if (androidContentContainer == null) return;

        // 首次初始化 ScrollView 和 LinearLayout
        if (scrollView == null) {
            scrollView = new ScrollView(appContext);
            lineContainer = new LinearLayout(appContext);
            lineContainer.setOrientation(LinearLayout.VERTICAL);
            scrollView.addView(lineContainer);
            scrollView.setFillViewport(true);

            ViewGroup debugViewContainer = androidContentContainer.findViewById(R.id.debugViewContainer);
            if (debugViewContainer != null) {
                debugViewContainer.removeAllViewsInLayout();
                debugViewContainer.addView(scrollView);
            }
        }

        // 动态添加日志 TextView
        TextView debugTextView = new TextView(appContext);
        debugTextView.setText(msg);
        if (lineContainer != null) {
            lineContainer.addView(debugTextView);
        }
    }

    public static void release() {
        if (scrollView != null) {
            scrollView = null;
        }

        if (lineContainer != null) {
            lineContainer = null;
        }
    }
}
